package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 数据统计服务
 */

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private SetmealDao setmealDao;
    //查询运用数据

    /**
     reportDate:null,
     todayNewMember :0,
     totalMember :0,
     thisWeekNewMember :0,
     thisMonthNewMember :0,
     todayOrderNumber :0,
     todayVisitsNumber :0,
     thisWeekOrderNumber :0,
     thisWeekVisitsNumber :0,
     thisMonthOrderNumber :0,
     thisMonthVisitsNumber :0,
     hotSetmeal :[
     {name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
     {name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}
     ]
     */
    public Map<String, Object> getBusinessReportData() throws Exception{
        //获得当前系统时间
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //动态获得本周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //动态获得本月第一天的日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //今日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);//select count(id) from t_member where regTime = today

        //总会员数据
        Integer totalMember = memberDao.findMemberTotalCount();

        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);

        //本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);

        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);

        //今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);

        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);

        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);

        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);

        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);

        //热门套餐
        List<Map> hotSetmeal = orderDao.findHotSetmeal();

        Map<String,Object> result = new HashMap<>();
        result.put("reportDate",today);
        result.put("todayNewMember",todayNewMember);
        result.put("totalMember",totalMember);
        result.put("thisWeekNewMember",thisWeekNewMember);
        result.put("thisMonthNewMember",thisMonthNewMember);
        result.put("todayOrderNumber",todayOrderNumber);
        result.put("thisWeekOrderNumber",thisWeekOrderNumber);
        result.put("thisMonthOrderNumber",thisMonthOrderNumber);
        result.put("todayVisitsNumber",todayVisitsNumber);
        result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        result.put("hotSetmeal",hotSetmeal);

        return result;
    }

    @Override
    public List<Map<String,Object>> getMemberReportData(Integer[] memberIds) {
        List<Map<String,Object>> list=new ArrayList();
        //遍历前台传过来的会员id数组
        for (Integer memberId : memberIds) {
            //创建map
            Map<String,Object> map=new HashMap();
            //根据会员id来查询会员信息,并把查询结果封装到member实体类中
            Member member = memberDao.findById(memberId);
            //将查询出来的会员对象存放进入map集合
            map.put("member",member);
            //创建list集合用来封装查询出来的套餐id和预约的地址信息
            List<Map<String,Object>> arrayList=new ArrayList<Map<String,Object>>();
            arrayList=orderDao.findSetmealIdAddressByMemberId(memberId);
            //创建list集合用来封装根据arrayList集合中的套餐id查询出来的套餐详情信息,以及ArrayList集合中的预约地址
//            List<Map<String,Object>> setmealAndAddressList=new ArrayList<Map<String,Object>>();
            List<Map<Setmeal,String>> setmealAndAddressList=new ArrayList<Map<Setmeal,String>>();
            //遍历ArrayList集合获取其中的每个元素map集合,包含套餐id,以及预约的地址
            for (Map<String,Object> setmealIdAndAddressMap : arrayList) {
                //获取套餐id
                Integer setmealId = (Integer) setmealIdAndAddressMap.get("setmealId");
                    //根据获取的套餐id信息来查询套餐的详细信息
                    Setmeal setmeal =setmealDao.findById4Detail(setmealId);
                    //获取map集合中的地址信息
                String address = (String) setmealIdAndAddressMap.get("address");
                //创建一个map集合用来封装查询出来的套餐以及预约地址
                    Map<Setmeal,String> setmealAndAddressMap=new HashMap<Setmeal,String>();
                    //把获取到的套餐的信息和预约地址封装到setmealAndAddressMap集合中
                    setmealAndAddressMap.put(setmeal,address);
                    //将每一个套餐信息,以及预约地址封装到setmealAndAddressList集合中
                    setmealAndAddressList.add(setmealAndAddressMap);
                }
            //将每个会员对应的套餐信息以及预约地址的信息封装到map集合中
            map.put("addressAndSetMeal",setmealAndAddressList);
            //将每一个会员的信息封装到list集合中
            list.add(map);
        }
        return list;
    }
}
